<?php
class Ms_siswa extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->helper('format');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('siswa_model');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
		$this->load->model('siswa_naik_kelas_model');
		$this->load->model('guru_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library('upload');
	}
	
	function index()
	{
		$data['value_tingkat'] = '';
		$data['value_kelas'] = '';
		
			if($this->input->post('tingkat')!=''){
				$this->session->set_userdata('sess_pg_tingkat', $this->input->post('tingkat'));
			}
			
			if($this->input->post('kelas')!='' ){
				$this->session->set_userdata('sess_pg_kelas', $this->input->post('kelas'));
			}
			
			$data['value_tingkat'] = $this->session->userdata('sess_pg_tingkat');
			$data['value_kelas'] = $this->session->userdata('sess_pg_kelas');
			
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['a.NISN'] = array('NIS',100,TRUE,'center',1);
		$colModel['NIPDN'] = array('NISN',100,TRUE,'center',1);
		$colModel['NAMA_LENGKAP'] = array('Nama lengkap',200,TRUE,'center',1);
		$colModel['TEMPAT_LAHIR'] = array('Tempat Lahir',80,TRUE,'center',1);
		$colModel['TANGGAL_LAHIR'] = array('Tanggal Lahir',80,TRUE,'center',1);
		$colModel['ALAMAT_DOMISILI'] = array('Alamat Domisili',300,TRUE,'center',1);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		$colModel['cetak'] = array('Cetak',50,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Siswa'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		// if($this->input->post('tingkat')!='' and $this->input->post('kelas')!='' )
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$buttons[] = array('Add','add','org_js');
		
		$buttons[] = array('Delete','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$buttons[] = array('Cetak Daftar Siswa','print','org_js');
		$buttons[] = array('separator');
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$buttons[] = array('Cetak Daftar Siswa (Excel)','xls','org_js');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$grid_js = build_grid_js('flex1',site_url("/ms_siswa/listsiswa_grid/".$data['value_tingkat']."/".$data['value_kelas']),$colModel,'no','asc',$gridParams,$buttons);
		else
			$grid_js = build_grid_js('flex1',site_url("/ms_siswa/listsiswa_grid/"),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					location.href='".base_url()."index.php/ms_siswa/input_siswa/".$data['value_tingkat']."/".$data['value_kelas']."'; 
				}
				
				if (com=='Cetak Daftar Siswa')
				{
					popup('".base_url()."index.php/ms_siswa/cetak_list_siswa/".$data['value_tingkat']."/".$data['value_kelas']."/html',1050,600);
				}
				
				if (com=='Cetak Daftar Siswa (Excel)')
				{
					location.href='".base_url()."index.php/ms_siswa/cetak_list_siswa/".$data['value_tingkat']."/".$data['value_kelas']."/xls'; 
				}
				
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/ms_siswa/delete_siswa')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			
		
			function popup(url,width,height) 
			{
				 var left   = (screen.width  - width)/2;
				 var top    = (screen.height - height)/2;
				 var params = 'width='+width+', height='+height;
				 params += ', top='+top+', left='+left;
				 params += ', directories=no';
				 params += ', location=no';
				 params += ', menubar=no';
				 params += ', resizable=no';
				 params += ', scrollbars=yes';
				 params += ', status=no';
				 params += ', toolbar=no';
				 newwin=window.open(url,'windowname5', params);
				 if (window.focus) {newwin.focus()}
				 return false;
			}
			
			
			</script>
			";	
			
		$data['tingkat'] = $this->master_model->combo_tingkat_kelas($this->session->userdata('unitid'));
		$data['kelas'] = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$data['value_tingkat']);
		
		$data['url_filter'] = 'ms_siswa/index';
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listsiswa_grid($id_tingkat='',$id_kelas='')
	{
		
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('a.NISN','NIPDN','NAMA_LENGKAP','TEMPAT_LAHIR','TANGGAL_LAHIR','ALAMAT_DOMISILI');
		
		$this->flexigrid->validate_post('a.NISN','asc',$valid_fields);
		
		$records = $this->siswa_model->get_siswa_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$id_tingkat,$id_kelas);
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		if($records['record_count'] != 0 ){
		 
			foreach ($records['records']->result() as $row)
			{
				
				$record_items[] = array($row->NISN,
				$no+1,
				'<div align="center">'.$row->NISN.'</div>',
				'<div align="center">'.$row->NIPDN.'</div>',
				'<div align="center">'.$row->NAMA_LENGKAP.'</div>',
				'<div align="left">'.$row->TEMPAT_LAHIR.'</div>',
				'<div align="left">'.xFormatDate($row->TANGGAL_LAHIR).'</div>',
				'<div align="left">'.$row->ALAMAT_DOMISILI.'</div>',
				'<a href=\''.site_url().'/ms_siswa/edit_siswa/'.$row->NISN.'/'.$id_tingkat.'/'.$id_kelas.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> ',
				'<a title="Cetak Detail Siswa" href=\''.site_url().'/ms_siswa/cetak_list_siswa/'.$id_tingkat.'/'.$id_kelas.'/pdf/'.$row->NISN.'/\'><img border=\'0\' src=\''.base_url().'images/report/pdf.gif\'></a> '
				);
				$no++;
			}
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {
			// Setting Rules
			$this->form_validation->set_rules('tahun_masuk', 'Tahun Masuk', 'required|numeric');
			$this->form_validation->set_rules('nisn', 'NISN', 'required|callback_validasi_nisn');
			$this->form_validation->set_rules('nipdn', 'NIPDN', 'numeric');
			$this->form_validation->set_rules('nama_lengkap', 'Nama Lengkap', 'required');
			$this->form_validation->set_rules('tempat_lahir', 'Tempat Lahir', 'required');
			// $this->form_validation->set_rules('tanggal_lahir', 'Tanggal Lahir', 'required|callback_validasi_date');
			$this->form_validation->set_rules('day', 'Tanggal Lahir', 'required|callback_validasi_date');
			$this->form_validation->set_rules('agama', 'Agama', 'required');
			$this->form_validation->set_rules('jenis_kelamin', 'Jenis Kelamin', 'required');
			$this->form_validation->set_rules('no_ktp', 'No KTP', 'numeric');
			$this->form_validation->set_rules('anak_ke', 'Anak ke', 'numeric');
			$this->form_validation->set_rules('dari_jumlah_saudara', 'dari', 'numeric');
			$this->form_validation->set_rules('telepon', 'Telepon', 'numeric');
			$this->form_validation->set_rules('hp', 'Handphone', 'numeric');
			$this->form_validation->set_rules('kodepos', 'Kodepos', 'numeric');
			// $this->form_validation->set_rules('berat_badan', 'Berat Badan', 'numeric');
			// $this->form_validation->set_rules('tinggi_badan', 'Tinggi Badan', 'numeric');
			// $this->form_validation->set_rules('telepon_ayah', 'Telepon Ayah', 'numeric');
			// $this->form_validation->set_rules('penghasilan_ayah', 'Penghasilan Ayah', 'numeric');
			// $this->form_validation->set_rules('hp_ayah', 'Handphone Ayah', 'numeric');
			// $this->form_validation->set_rules('telepon_ibu', 'Telepon Ibu', 'numeric');
			// $this->form_validation->set_rules('penghasilan_ayah', 'Penghasilan Ayah', 'numeric');
			// $this->form_validation->set_rules('hp_ibu', 'Handphone Ibu', 'numeric');
			// $this->form_validation->set_rules('penghasilan_ibu', 'Penghasilan Ibu', 'numeric');
			// $this->form_validation->set_rules('telepon_wali', 'Telepon Wali', 'numeric');
			// $this->form_validation->set_rules('hp_wali', 'Handphone Wali', 'numeric');
			// $this->form_validation->set_rules('penghasilan_wali', 'Penghasilan Wali', 'numeric');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			$this->form_validation->set_message('numeric', 'Kolom %s harus angka!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function validasi_nisn(){
		$nisn = $this->input->post('nisn');
		
		$success = $this->siswa_model->get_idsiswa($nisn);
		
		if ($success == '0'){
		  return TRUE;
		}
		else{
		  $this->form_validation->set_message('validasi_nisn', 'NIS sudah terdaftar.');
		  return FALSE;
		}
	}
	
	function validasi_date(){
		$day = $this->input->post('day');
		$month = $this->input->post('month');
		$year = $this->input->post('year');
		if (checkdate($month, $day, $year)){
		  return TRUE;
		}
		else{
		  $this->form_validation->set_message('validasi_date', 'Masukkan tanggal lahir yang benar.');
		  return FALSE;
		}
	}
	
	// function cek_validasi_kesehatan() {	
			// // Setting Rules
			// $this->form_validation->set_rules('golongan_darah', 'Golongan Darah', 'required');
			// $this->form_validation->set_rules('berat_badan', 'Berat Badan', 'required');
			// $this->form_validation->set_rules('tinggi_badan', 'Tinggi Badan', 'required');
			
			// // Setting Error Message
			// $this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// // Setting Delimiter
			// $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			// return $this->form_validation->run();
	// }
	
	// function cek_validasi_ortu() {	
			// // Setting Rules
			// $this->form_validation->set_rules('nama_ayah', 'Golongan Darah', 'required');
			// $this->form_validation->set_rules('is_ayah_almarhum', 'Berat Badan', 'required');
			// $this->form_validation->set_rules('pendidikan_ayah', 'Tinggi Badan', 'required');
			// $this->form_validation->set_rules('pekerjaan_ayah', 'Tinggi', 'required');
			// $this->form_validation->set_rules('penghasilan_ayah', 'Tinggi', 'required');
			// $this->form_validation->set_rules('telepon_ayah', 'Tinggi', 'required');
			// $this->form_validation->set_rules('hp_ayah', 'Tinggi', 'required');
			// $this->form_validation->set_rules('email_ayah', 'Tinggi', 'required');
			// $this->form_validation->set_rules('nama_ibu', 'Tinggi', 'required');
			// $this->form_validation->set_rules('nama_ibu', 'Tinggi', 'required');
			
			// // Setting Error Message
			// $this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// // Setting Delimiter
			// $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			// return $this->form_validation->run();
	// }
	
	function input_siswa($tingkat,$kelas)
    {
		if ($this->cek_validasi() == FALSE){
			//tambahan untuk tambah yang duplikat memasukkan primary key
			$data['agamaval'] = $this->input->post('agama');
			$data['bahasaval'] = $this->input->post('bahasa');
			$data['yearval'] = $this->input->post('year');
			$data['monthval'] = $this->input->post('month');
			$data['dayval'] = $this->input->post('day');
			
			$data['status'] = 'new';
			$data['id_tingkat'] = $tingkat;
			$data['id_kelas'] = $kelas;
			$data['agama'] = $this->guru_model->combo_agama();
			// $data['kondisi'] = $this->master_model->combo_kondisi();
			$data['bahasa'] = $this->master_model->combo_bahasa();
			$data['pekerjaan'] = $this->master_model->combo_pekerjaan();
			$data['pendidikan'] = $this->master_model->combo_pendidikan();
			$data['content'] = $this->load->view('form_siswa',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$countsiswa = $this->siswa_model->get_count_siswa($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$tingkat,$kelas);
			$capacity = $this->siswa_model->get_capacity_class($kelas);
			
			// echo $countsiswa." ".$capacity;exit;
			if($countsiswa<=$capacity)
				$this->insert_siswa($tingkat,$kelas);
			else {
				$data['err_msg'] = 'Maaf, kelas sudah terisi penuh';
				$data['status'] = 'new';
				$data['id_tingkat'] = $tingkat;
				$data['id_kelas'] = $kelas;
				$data['agama'] = $this->guru_model->combo_agama();
				// $data['kondisi'] = $this->master_model->combo_kondisi();
				$data['bahasa'] = $this->master_model->combo_bahasa();
				$data['pekerjaan'] = $this->master_model->combo_pekerjaan();
				$data['pendidikan'] = $this->master_model->combo_pendidikan();
				$data['content'] = $this->load->view('form_siswa',$data,true);
				$this->load->view('main',$data);
			}
		}            
    }
	
	function insert_siswa($tingkat,$kelas){
		$tgl_lahir = $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day');
		$filenamees = explode('.',$_FILES['userfile']['name']);
		$filenamess = $filenamees[count($filenamees)-1];
		
		$data = array(
			'ID_SEKOLAH'			=> $this->session->userdata('unitid'),
			'TAHUN_MASUK'			=> $this->input->post('tahun_masuk'),
			'NISN'					=> $this->input->post('nisn'),
			'NIPDN'					=> $this->input->post('nipdn'),
			'NAMA_LENGKAP'			=> $this->input->post('nama_lengkap'),
			'NAMA_PANGGILAN'		=> $this->input->post('nama_panggilan'),
			'JENIS_KELAMIN'	 		=> $this->input->post('jenis_kelamin'),
			'TEMPAT_LAHIR'	 		=> $this->input->post('tempat_lahir'),
			// 'TANGGAL_LAHIR'	 		=> xFormatDate($this->input->post('tanggal_lahir'),'/','-'),
			'TANGGAL_LAHIR'	 		=> $tgl_lahir,
			'AGAMA'			 		=> $this->input->post('agama'),
			// 'KONDISI_KELUARGA' 		=> $this->input->post('kondisi_keluarga'),
			'NAMA_WNA'		 		=> $this->input->post('nama_wna'),
			'WARGA_NEGARA'	 		=> $this->input->post('warga_negara'),
			'ANAK_KE'		 		=> $this->input->post('anak_ke'),
			'DARI_JUMLAH_SAUDARA'	=> $this->input->post('dari_jumlah_saudara'),
			'BAHASA'		 		=> $this->input->post('bahasa'),
			'ALAMAT_ASAL'	 		=> $this->input->post('alamat_asal'),
			'ALAMAT_DOMISILI' 		=> $this->input->post('alamat_domisili'),
			'KODEPOS'		 		=> $this->input->post('kodepos'),
			'TELEPON'		 		=> $this->input->post('telepon'),
			'HP'		 			=> $this->input->post('hp'),
			'EMAIL'		 			=> $this->input->post('email'),
			'ASAL_SEKOLAH' 			=> $this->input->post('asal_sekolah'),
			'KETERANGAN_ASAL_SEKOLAH'	=> $this->input->post('keterangan_asal_sekolah')
		);
		
		$data_kelas = array(
			'ID_SEKOLAH'			=> $this->session->userdata('unitid'),
			'ID_TAHUNAJARAN'		=> $this->session->userdata('tahunajaran'),
			'ID_TINGKAT'			=> $tingkat,
			'ID_KELAS'				=> $kelas,
			'NISN'					=> $this->input->post('nisn')
		);
		$ins_siswa = $this->siswa_model->insert_siswa($data);
		
		if($ins_siswa == true){
			$ins_siswa = $this->siswa_model->insert_siswa_kelas($data_kelas);
		}
		
		if($ins_siswa == true){
				if($_FILES['userfile']['name']!= ''){
					$config['file_name'] = $this->input->post('nisn');
					$config['upload_path'] = './uploads/siswa/';
					$config['allowed_types'] = 'gif|jpg|png|jpeg';
					$config['max_size']	= '500';
					$this->upload->initialize($config);
					$this->load->library('upload', $config);
					
					if ( ! $this->upload->do_upload()){
						$data['err_msg'] = 'Maaf, file yang diupload tidak sesuai dengan format';
						$data['status'] = 'edit';
						$data['id_tingkat'] = $tingkat;
						$data['id_kelas'] = $kelas;
						$data['agama'] = $this->guru_model->combo_agama();
						$data['bahasa'] = $this->master_model->combo_bahasa();
						$data['pekerjaan'] = $this->master_model->combo_pekerjaan();
						$data['pendidikan'] = $this->master_model->combo_pendidikan();
						$data['rs_siswa'] = $this->siswa_model->get_siswaID($this->input->post('nisn'));
						$data['content'] = $this->load->view('form_siswa',$data,true);
						$this->load->view('main',$data);
					}
					else {	
							$data2 = array();
							$data2['FOTO'] = $this->input->post('nisn').'.'.$filenamess;
							$this->siswa_model->update_siswa($data2,$this->input->post('nisn'));
							
							$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
							redirect('ms_siswa');
						}				
				}else {	
						
						$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
						redirect('ms_siswa');
					}
		}
	}
	
	function edit_siswa($nisn,$id_tingkat, $id_kelas)
    {
 		if ($this->cek_validasi() == FALSE){
				$data['status'] = 'edit';
				$data['id_tingkat'] = $id_tingkat;
				$data['id_kelas'] = $id_kelas;
				$data['agama'] = $this->guru_model->combo_agama();
				// $data['kondisi'] = $this->master_model->combo_kondisi();
				$data['bahasa'] = $this->master_model->combo_bahasa();
				$data['pekerjaan'] = $this->master_model->combo_pekerjaan();
				$data['pendidikan'] = $this->master_model->combo_pendidikan();
				$data['rs_siswa'] = $this->siswa_model->get_siswaID($nisn);
				$data['content'] = $this->load->view('form_siswa',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$this->update_siswa($nisn,$id_tingkat, $id_kelas);
			}            
    }
	
	function update_siswa($nisn,$id_tingkat, $id_kelas){
		
		$tgl_lahir = $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day');
		$filenamees = explode('.',$_FILES['userfile']['name']);
		$filenamess = $filenamees[count($filenamees)-1];
		
		$data = array(
			'TAHUN_MASUK'			=> $this->input->post('tahun_masuk'),
			'NISN'					=> $this->input->post('nisn'),
			'NIPDN'					=> $this->input->post('nipdn'),
			'NAMA_LENGKAP'			=> $this->input->post('nama_lengkap'),
			'NAMA_PANGGILAN'		=> $this->input->post('nama_panggilan'),
			'JENIS_KELAMIN'	 		=> $this->input->post('jenis_kelamin'),
			'TEMPAT_LAHIR'	 		=> $this->input->post('tempat_lahir'),
			// 'TANGGAL_LAHIR'	 		=> xFormatDate($this->input->post('tanggal_lahir'),'/','-'),
			'TANGGAL_LAHIR'	 		=> $tgl_lahir,
			'AGAMA'			 		=> $this->input->post('agama'),
			// 'KONDISI_KELUARGA' 		=> $this->input->post('kondisi_keluarga'),
			'WARGA_NEGARA'	 		=> $this->input->post('warga_negara'),
			'NAMA_WNA'		 		=> $this->input->post('nama_wna'),
			'ANAK_KE'		 		=> $this->input->post('anak_ke'),
			'DARI_JUMLAH_SAUDARA'	=> $this->input->post('dari_jumlah_saudara'),
			'BAHASA'		 		=> $this->input->post('bahasa'),
			'ALAMAT_ASAL'	 		=> $this->input->post('alamat_asal'),
			'ALAMAT_DOMISILI' 		=> $this->input->post('alamat_domisili'),
			'KODEPOS'		 		=> $this->input->post('kodepos'),
			'TELEPON'		 		=> $this->input->post('telepon'),
			'HP'		 			=> $this->input->post('hp'),
			'EMAIL'		 			=> $this->input->post('email'),
			'ASAL_SEKOLAH' 			=> $this->input->post('asal_sekolah'),
			'KETERANGAN_ASAL_SEKOLAH'	=> $this->input->post('keterangan_asal_sekolah')
		);
			$this->siswa_model->update_siswa($data,$nisn);
			
			if($_FILES['userfile']['name']!= ''){
				$config['file_name'] = $this->input->post('nisn');
				$config['upload_path'] = './uploads/siswa/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$config['max_size']	= '500';
				$this->upload->initialize($config);
				$this->load->library('upload', $config);
				
				//delete files //
				if($this->input->post('fileold')!= ''){
					unlink('uploads/siswa/'.$this->input->post('fileold'));
				}
				if ( ! $this->upload->do_upload()){
					$data['err_msg'] = 'Maaf, file yang diupload tidak sesuai dengan format';
					$data['status'] = 'edit';
					$data['id_tingkat'] = $id_tingkat;
					$data['id_kelas'] = $id_kelas;
					$data['agama'] = $this->guru_model->combo_agama();
					$data['bahasa'] = $this->master_model->combo_bahasa();
					$data['pekerjaan'] = $this->master_model->combo_pekerjaan();
					$data['pendidikan'] = $this->master_model->combo_pendidikan();
					$data['rs_siswa'] = $this->siswa_model->get_siswaID($nisn);
					$data['content'] = $this->load->view('form_siswa',$data,true);
					$this->load->view('main',$data);
				}
				else {	
						$data2 = array();
						$data2['FOTO'] = $this->input->post('nisn').'.'.$filenamess;
						$this->siswa_model->update_siswa($data2,$nisn);
						
						$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
						redirect('ms_siswa');
					}
			}
			else {	
					$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
					redirect('ms_siswa'); 
				}
	}
	
	
	function edit_siswa_kesehatan($nisn)
    {
 		// if ($this->cek_validasi_kesehatan() == FALSE){
				// $data['status'] = 'edit';
				// $data['rs_siswa'] = $this->siswa_model->get_siswaID($nisn);
				// $data['content'] = $this->load->view('form_siswa',$data,true);
				// $this->load->view('main',$data);
			// }
			// else{
				$this->update_siswa_kesehatan($nisn);
			// }            
    }
	
	function update_siswa_kesehatan($nisn){
		$data = array(
			'GOLONGAN_DARAH'			=> $this->input->post('golongan_darah'),
			'BERAT_BADAN'					=> $this->input->post('berat_badan'),
			'TINGGI_BADAN'			=> $this->input->post('tinggi_badan'),
			'PENYAKIT_YANG_DIDERITA'	=> $this->input->post('penyakit_yang_diderita')
		);
		// var_dump($_POST);exit;
		$this->siswa_model->update_siswa($data,$nisn);
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('ms_siswa'); 
	}
	
	
	function edit_siswa_ortu($nisn)
    {
 		// if ($this->cek_validasi_ortu() == FALSE){
				// $data['status'] = 'edit';
				// $data['rs_siswa'] = $this->siswa_model->get_siswaID($nisn);
				// $data['content'] = $this->load->view('form_siswa',$data,true);
				// $this->load->view('main',$data);
			// }
			// else{
				$this->update_siswa_ortu($nisn);
			// }            
    }
	
	function update_siswa_ortu($nisn){
		$data = array(
			'NAMA_AYAH'			=> $this->input->post('nama_ayah'),
			'IS_AYAH_ALMARHUM'	=> $this->input->post('is_ayah_almarhum'),
			'PENDIDIKAN_AYAH'	=> $this->input->post('pendidikan_ayah'),
			'PEKERJAAN_AYAH'	=> $this->input->post('pekerjaan_ayah'),
			'PENGHASILAN_AYAH'	=> $this->input->post('penghasilan_ayah'),
			'TELEPON_AYAH'	 	=> $this->input->post('telepon_ayah'),
			'HP_AYAH'	 		=> $this->input->post('hp_ayah'),
			'EMAIL_AYAH'		=> $this->input->post('email_ayah'),
			'NAMA_IBU'			=> $this->input->post('nama_ibu'),
			'IS_IBU_ALMARHUM'	=> $this->input->post('is_ibu_almarhum'),
			'PENDIDIKAN_IBU'	=> $this->input->post('pendidikan_ibu'),
			'PEKERJAAN_IBU'		=> $this->input->post('pekerjaan_ibu'),
			'PENGHASILAN_IBU'	=> $this->input->post('penghasilan_ibu'),
			'TELEPON_IBU'	 	=> $this->input->post('telepon_ibu'),
			'HP_IBU'	 		=> $this->input->post('hp_ibu'),
			'EMAIL_IBU'			=> $this->input->post('email_ibu'),
			'NAMA_WALI'			=> $this->input->post('nama_wali'),
			'PENDIDIKAN_WALI'	=> $this->input->post('pendidikan_wali'),
			'PEKERJAAN_WALI'	=> $this->input->post('pekerjaan_wali'),
			'PENGHASILAN_WALI'	=> $this->input->post('penghasilan_wali'),
			'TELEPON_WALI'	 	=> $this->input->post('telepon_wali'),
			'HP_WALI'	 		=> $this->input->post('hp_wali'),
			'EMAIL_WALI'		=> $this->input->post('email_wali'),
			'ALAMAT_WALI'		=> $this->input->post('alamat_wali'),
			'KETERANGAN'		=> $this->input->post('keterangan')
		);
		// var_dump($_POST);exit;
		$this->siswa_model->update_siswa($data,$nisn);
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('ms_siswa'); 
	}
	
	//Delete
	function delete_siswa()
	{
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->siswa_model->delete_siswa($ta_id);
				$this->siswa_model->delete_siswa_kelas($ta_id);
			}
		}$msg .= "Data Siswa berhasil dihapus.\n";
			
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
    
	//fungsi untuk membuat tingkat dan kelas menjadi combobox yang dinamis
    
    function get_daftar_kelas() {
		if ($_POST['tingkat_id']!='') {
            $parent = $_POST['tingkat_id'];
            $kelas = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$parent);
            $options_kelas[''] = "-- Pilih Kelas --";
			foreach($kelas->result() as $rows){
				$options_kelas[$rows->ID_KELAS] = $rows->KELAS; 
			}
			echo form_dropdown('kelas',$options_kelas,$this->session->userdata('sess_kelas_tujuan'),'id="kelas" class="field select"');
        }
        else
            echo '<select id="kelas" class="field select" name="kelas">
				<option selected="selected" value="">-- Pilih Kelas --</option>
				</select>';
    }//end function
	
	function set_session_kelas_tujuan(){
		$this->session->set_userdata('sess_kelas_tujuan', $_POST['tujuan_kelas']);
	}
	
	function set_session_jenis_kelulusan(){
		$this->session->set_userdata('sess_jenis_lulus', $_POST['jns_lulus']);
	}
	
	function set_session_keterangan_kelulusan(){
		$this->session->set_userdata('sess_keterangan_lulus', $_POST['ket_lulus']);
	}
	
	function set_session_jenis_kenaikan(){
		$this->session->set_userdata('sess_jenis_naik', $_POST['jns_naik']);
		$tahun_next = $_POST['tahun_id'];
		
		if ($_POST['jns_naik']=='2') {
            $parent = $_POST['tingkat_id'];
            $kelas_tujuan_naik = $this->master_model->combo_kelas($this->session->userdata('unitid'),$tahun_next,$parent);
            $options_kelas_tujuan_naik[''] = "-- Pilih Kelas Tujuan--";
			foreach($kelas_tujuan_naik->result() as $rows){
				$options_kelas_tujuan_naik[$rows->ID_KELAS] = $rows->KELAS; 
			}
			echo form_dropdown('kelas_tujuan_naik',$options_kelas_tujuan_naik,$this->session->userdata('sess_kelas_tujuan_naik'),'id="kelas_tujuan_naik" class="field select" onChange="change_kelas_tujuan_naik()"');
        }
		else if ($_POST['jns_naik']=='1') {
            $parent = $_POST['tingkat_id'];
            $kelas_tujuan_naik = $this->siswa_naik_kelas_model->combo_kelas($this->session->userdata('unitid'),$tahun_next,$parent);
			 
			$options_kelas_tujuan_naik[''] = "-- Pilih Kelas Tujuan--";
			foreach($kelas_tujuan_naik->result() as $rows){
				$options_kelas_tujuan_naik[$rows->ID_KELAS] = $rows->KELAS; 
			}
			echo form_dropdown('kelas_tujuan_naik',$options_kelas_tujuan_naik,$this->session->userdata('sess_kelas_tujuan_naik'),'id="kelas_tujuan_naik" class="field select" onChange="change_kelas_tujuan_naik()"');
        }
        else
            echo '<select id="kelas_tujuan_naik" class="field select" name="kelas_tujuan_naik" onChange="change_kelas_tujuan_naik()">
				<option selected="selected" value="">-- Pilih Kelas Tujuan--</option>
				</select>';
	}
	
	function set_session_keterangan_kenaikan(){
		$this->session->set_userdata('sess_keterangan_naik', $_POST['ket_naik']);
	}
	
	function set_session_tahunajaran_naik(){
		$this->session->set_userdata('sess_tahun_ajaran_naik', $_POST['tahun_ajaran_naik']);
	}
	
	function set_session_kelas_tujuan_naik(){
		$this->session->set_userdata('sess_kelas_tujuan_naik', $_POST['kelas_tujuan_naik']);
	}
	function cetak_list_siswa($id_tk, $id_kls, $type, $nisn=''){
		$nis = $nisn;
		// var_dump($nis);
		if($nis=='' && $type =='xls'){
			$data['filename']="daftar_siswa";
			$data['type'] = $type;
			$data['siswa'] = $this->siswa_model->get_siswa_laporan($id_tk, $id_kls);
			$this->load->view('cetak_daftar_siswa',$data);
		}else if($nis=='' && $type =='html'){
			$data['filename']="daftar_siswa";
			$data['type'] = $type;
			$data['siswa'] = $this->siswa_model->get_siswa_laporan($id_tk, $id_kls);
			$this->load->view('cetak_daftar_siswa_html',$data);
		}else{
			// var_dump($nis);
			$this->load->helper('to_pdf');
			$this->load->helper('file');
			$filename="detail_siswa_".$nis;
			$data['type'] = 'pdf';
			$data['siswa'] = $this->siswa_model->get_siswaID($nis)->row();
			
			// $data['kondisi'] = $this->master_model->combo_kondisi($data['siswa']->KONDISI_KELUARGA)->row();
			$data['agama'] = $this->master_model->combo_agama($data['siswa']->AGAMA)->row();
			$data['bahasa'] = $this->master_model->combo_bahasa($data['siswa']->BAHASA)->row();
			$data['r_pekerjaan_ayah'] = $this->master_model->combo_pekerjaan($data['siswa']->PEKERJAAN_AYAH)->row();
			$data['r_pekerjaan_ibu'] = $this->master_model->combo_pekerjaan($data['siswa']->PEKERJAAN_IBU)->row();
			$data['r_pekerjaan_wali'] = $this->master_model->combo_pekerjaan($data['siswa']->PEKERJAAN_WALI)->row();
			$data['r_pendidikan_ayah'] = $this->master_model->combo_pendidikan($data['siswa']->PENDIDIKAN_AYAH)->row();
			$data['r_pendidikan_ibu'] = $this->master_model->combo_pendidikan($data['siswa']->PENDIDIKAN_IBU)->row();
			$data['r_pendidikan_wali'] = $this->master_model->combo_pendidikan($data['siswa']->PENDIDIKAN_WALI)->row();
			
			$html = $this->load->view('cetak_detail_siswa',$data,TRUE);
			pdf_create($html, $filename);
		}
	}	
}
?>
